package com.catering.controller;

import com.catering.entity.UserAddress;
import com.catering.service.UserAddressService;
import com.catering.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import lombok.extern.slf4j.Slf4j;  // 添加这行

@Slf4j  // 添加这个注解
@RestController
@RequestMapping("/address")
public class UserAddressController {

    @Autowired
    private UserAddressService addressService;

    /**
     * 获取用户地址列表
     */
    @GetMapping("/list")
    public R<List<UserAddress>> list(@RequestParam Long userId) {
        log.info("获取用户地址列表，userId: {}", userId);
        List<UserAddress> list = addressService.lambdaQuery()
                .eq(UserAddress::getUserId, userId)
                .list();
        return R.ok(list);
    }

    /**
     * 添加地址
     */
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody UserAddress address) {
        log.info("添加地址请求：{}", address);
        try {
            boolean success = addressService.saveAddress(address);
            if (success) {
                log.info("添加地址成功");
                return R.ok(true);
            } else {
                log.error("添加地址失败");
                return R.fail("添加地址失败");
            }
        } catch (Exception e) {
            log.error("添加地址异常：", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新地址
     */
    @PutMapping("/update/{id}")
    public R<Boolean> update(@PathVariable Long id, @RequestBody UserAddress address) {
        log.info("更新地址请求：id={}, address={}", id, address);
        try {
            address.setId(id);
            boolean success = addressService.saveAddress(address);
            if (success) {
                log.info("更新地址成功");
                return R.ok(true);
            } else {
                log.error("更新地址失败");
                return R.fail("更新地址失败");
            }
        } catch (Exception e) {
            log.error("更新地址异常：", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        log.info("删除地址请求：id={}", id);
        try {
            boolean success = addressService.removeById(id);
            if (success) {
                log.info("删除地址成功");
                return R.ok(true);
            } else {
                log.error("删除地址失败");
                return R.fail("删除地址失败");
            }
        } catch (Exception e) {
            log.error("删除地址异常：", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/setDefault/{id}")
    public R<Boolean> setDefault(@PathVariable Long id, @RequestParam Long userId) {
        log.info("设置默认地址请求：id={}, userId={}", id, userId);
        try {
            // 先将所有地址设为非默认
            addressService.lambdaUpdate()
                    .eq(UserAddress::getUserId, userId)
                    .set(UserAddress::getIsDefault, false)
                    .update();

            // 设置指定地址为默认
            boolean success = addressService.lambdaUpdate()
                    .eq(UserAddress::getId, id)
                    .set(UserAddress::getIsDefault, true)
                    .update();

            if (success) {
                log.info("设置默认地址成功");
                return R.ok(true);
            } else {
                log.error("设置默认地址失败");
                return R.fail("设置默认地址失败");
            }
        } catch (Exception e) {
            log.error("设置默认地址异常：", e);
            return R.fail(e.getMessage());
        }
    }
}
